package com.damai.mvc.view.imple;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.beetl.core.Template;
import org.beetl.core.exception.BeetlException;

import com.damai.error.ErrorUtil;
import com.damai.mvc.view.ErrorRender;
import com.damai.mvc.view.View;


public class WebErrorRender implements ErrorRender {

	public static final WebErrorRender DEFAULT = new WebErrorRender();
	
	@Override
	public void renderError(Throwable e, HttpServletRequest request, HttpServletResponse response) throws IOException {
		//找一个视图
		e.printStackTrace();
		
		if(e instanceof View){
			((View)e).renderTo(response);
			return;
		}
		
		Template template = DefaultWebRender.getTemplate("error/exception.html");
		//打印出来
		String ex= ErrorUtil.printException(e);
		template.binding("exception",ex);
		
		try {
			template.renderTo(response.getOutputStream());
		} catch (BeetlException e1) {
			throw new RuntimeException(e1);
		}
		
	}

}
